package com.talkative.plan.util;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * @author : best
 * @date : 2022/10/3
 */
public class AliSmsUtil {
    /**客户端对象*/
    public static Client client;
    /**账号的key -保密*/
    public static final String accessKeyId = "LTAI5t5ZPUfK5nTEUPCwzSxP";
    /**账号的密钥 -保密*/
    public static final String accessKeySecret = "BdWzLO0B8Z7YhzBK3Hx2CvQJudyo32";
    static {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";// 访问的域名
        try {
            client=new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 发送短信
     * @param phone 手机号
     * @param code 验证码
     * @return 发送是否成功*/
    public static boolean sendCode(String phone,int code){
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("来自邢朋辉的短信")
                .setTemplateCode("SMS_115250125")
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response=client.sendSmsWithOptions(sendSmsRequest, runtime);
            return response.body.getCode().equals("OK");
        } catch (Exception error) {
        }
        return false;
    }
}
